<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * GXCMS
 *
 * Content Management System functions.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Applications
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 * @uses		GXClass
 */

class GXCMS extends GXClass {

	// Object var to ensure only one instance of this object
    private static $instance = NULL;
	// Method to call to instantiate this class
	public static function getInstance( $className = __CLASS__ ){
		if( !isset(self::$instance) ){
			self::$instance = new $className();
		}
		return self::$instance;
	}

	function __construct(){
		return parent::__construct();
	}

	private static function __destroy(){
		unset(self::$instance);
	}

	public function saveContent( $savePage = null, $str = null ){
		$pagePath = $this->GXPage->Layout->FS->Pages .$savePage .'.xml';
		if( !is_file($pagePath) ){
			trigger_error( 'Missing the requested page: ' .$savePage, E_USER_ERROR );
			return false;
		}

		// perform the replace
		$source = new GXDOM( $pagePath );
		// str is an array of keys which are xpaths and values which are content...there's really only 1
		foreach( $str as $xpath => $txt ){
			// it's urlencoded as sent from the browser
			$xpath = urldecode($xpath);
			// if magic quotes, strip slashes
			if( get_magic_quotes_gpc() ){
				$txt = stripslashes($txt);
			}
			// read in the new content
			$contentDoc = new GXDOM( 'Content', $txt );
			// find the node we're going to replace
			$old = $source->exec->xpath( $xpath, 0 );
			$old->load( $contentDoc, GXDOM::NO_CONTAINER, GXDOM::REPLACE_CALLER );
		}

		// write the results
		if( !@file_put_contents($pagePath, $source->exec->xml()) ){
			trigger_error( 'Unable to save content to this Page. Is the file writeable by the web server?', E_USER_ERROR );
			return false;
		}

		trigger_error( 'Your changes have been saved.', E_USER_DISPLAY );

		return true;
	}

	public function showImageList(){
		// get the images directory path
		if( !($dir = $this->GXPage->Layout->FS->Images) ){
			trigger_error( 'Unable to obtain path for Images directory', E_USER_ERROR );
			return false;
		}

		// validate it; returns full system path
		if( !($path = $this->GXPage->validatePath($dir)) ){
			trigger_error( 'Not a valid path', E_USER_ERROR );
			return false;
		}

		// get list of jpeg, png and gif files
		$list = glob($path .'/*.[jpg]*');
		if( !count($list) ){
			trigger_error( 'No jpeg, png or gif files were found', E_USER_WARNING );
		}
		for( $i = 0; $i < count($list); $i++ ){
			$img[] = array(
				'Name' => basename($list[$i]),
				'Path' => $list[$i],
				'src' => $dir .'/' .basename($list[$i])
				);
		}
		// return the doc
		$doc = new GXDOM('ImageList',$img);
		return $doc;
	}

	public function updatePicture( $savePage = null, $picture = null ){
		if( !$savePage || !$picture ){
			trigger_error( 'Unable to update the Picture (missing a parameter)', E_USER_WARNING );
			return false;
		}
		$pagePath = $this->GXPage->Layout->FS->Pages .$savePage .'.xml';
		if( !is_file($pagePath) ){
			trigger_error( 'Missing the requested page: ' .$savePage, E_USER_ERROR );
			return false;
		}

		// perform the replace
		$source = new GXDOM( $pagePath );
		foreach( $picture as $xpath => $name ){
			// it's urlencoded as sent from the browser
			$xpath = urldecode($xpath);
			// if magic quotes, strip slashes
			if( get_magic_quotes_gpc() ){
				$name = stripslashes($name);
			}
			// create a new doc from the picture name
			$pictureDoc = new GXDOM( 'Picture', null, array('Name'=>$name) );
			// find the node we're going to replace
			$old = $source->exec->xpath( $xpath, 0 );
			// replace the
			$old->load( $pictureDoc, GXDOM::NO_CONTAINER, GXDOM::REPLACE_CALLER );
		}

		// write the results
		if( !@file_put_contents($pagePath, $source->xml()) ){
			trigger_error( 'Unable to save content to this Page. Is the file writeable by the web server?', E_USER_ERROR );
			return false;
		}

		trigger_error( 'Your changes have been saved.', E_USER_DISPLAY );

		// return the path to the new image
		$xml = new TDOM('CMSPart',$this->GXPage->Layout->FS->Images .$name);
		return $xml;
	}

	public function uploadImage( $img = null ){
		printArray($_POST);
		printArray($_FILES);
		exit();
	}

}

?>
